<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Triveni Examples: Solutions</title>
  <link rel="StyleSheet" href="../trull.css" type="text/css">
</head>
<body bgcolor="#ffffff">
<h1 class="Course-Title">Trull Examples: Solutions</h1>
<h2 class="Course-Title">TRACING SOME PROGRAMS</h2>
<p>Trace each of the following programs --- A trace is the entire
sequence of events in the order in which they occur in a specific run of
the program. For each program, if there is only one possible trace,
then provide that one. Otherwise provide at least two different traces.
Assume that there are <em>no outside events</em> ; that is, the only
events in a program are the ones emitted by the program itself.</p>
<h3 class="Subproblem">Program:<em> </em></h3>
<pre class="Subproblem"><em>          emit a <br>       || emit b <br>       || await a -&gt; emit c</em></pre>
<p class="Answer">Trace 1: a b c</p>
<p class="Answer">Trace 2: a c b</p>
<p class="Answer">Trace 3: b a c</p>
<h3 class="Subproblem">Program:<em> </em></h3>
<pre class="Subproblem"><em>          emit a ; emit b <br>       || await a -&gt; emit c</em></pre>
<p class="Answer">Trace 1: a b c</p>
<p class="Answer">Trace 2: a c b </p>
<h3 class="Subproblem">Program:<em> </em></h3>
<pre class="Subproblem"><em>           emit a <br>        || emit b <br>        || do emit c  watching b</em> </pre>
<p class="Answer">Trace 1: a c b</p>
<p class="Answer">Trace 2: a b</p>
<h3 class="Subproblem">Program:</h3>
<pre class="Subproblem"><em>           emit a <br>        || emit b <br>        || do emit a watching b</em> </pre>
<p class="Answer">Trace 1: a b a</p>
<p class="Answer">Trace 2: a b</p>
<h3 class="Subproblem">Program:</h3>
<pre class="Subproblem"><em>           do [ emit a ] watching b <br>        || do [ emit b ] watching a</em> </pre>
<p class="Answer">Trace 1: a </p>
<p class="Answer">Trace 2: b</p>
<h3 class="Subproblem">Program:<em> </em></h3>
<pre class="Subproblem"><em>            emit a <br>         || emit b <br>         || await a -&gt; await b -&gt; emit c </em></pre>
<p class="Answer">Trace 1: a b c</p>
<p class="Answer">Trace 2: b a</p>
<h3 class="Subproblem">Program:<em> </em></h3>
<pre class="Subproblem"><em>         loop <br>           [ <br>             await a -&gt; emit b <br>           ]<br>         || <br>         loop <br>           [ <br>             await a -&gt; emit b <br>           ] <br>         || emit a</em> </pre>
<p class="Answer">Trace 1: a b b</p>
<p class="Answer">Trace 2:</p>
<h2>DEFINING NEW Trull COMBINATORS</h2>
<p>In this question, you are asked to write several parameterized
higher-level combinators expressed in terms of the basic Trull
combinators. Such combinators are used to build new Trull expressions
from existing ones.</p>
<h4 class="Paragraph">EXAMPLE (TOGGLE)</h4>
<p class="Paragraph">As a familiar example, the <em>toggle</em>
combinator is parameterized by an event <em>e</em> and two expressions <em>f</em>
and <em>s</em> . It starts as <em>f</em> and, whenever <em>e</em>
occurs, repeatedly toggles between <em>f</em> and <em>s</em> .</p>
<pre class="Program">toggle e f s = <br>    loop<br>        [ do f watching e <br>         ||<br>          await e --&gt; do s watching e<br>        ]</pre>
<h4 class="Program">STARTSTOP</h4>
<p class="Program">Write a combinator <em>StartStop</em> parameterized
by an event <em>START</em> , an event <em>STOP</em> , and an expression <em>e</em>
. This combinator indefinitely repeats the following behavior: When <em>START</em>
occurs, it should continue as <em>e</em> . When <em>STOP</em> occurs, it
should kill <em>e</em> , and so on. In other words, this combinator adds
to <em>e</em> the capability of being (re)started and stopped many
times.</p>
<pre class="Program">StartStop START STOP e =<br>     loop <br>            do [await START ---&gt; e ]<br>            watching STOP<br>            <br></pre>
<h3 class="Subproblem">COUNTDOWN</h3>
<p class="Subproblem">Write a combinator <em>CountDown</em>
parameterized by an event <em>TICK</em> , an integer <em>n</em> , and an
event <em>DONE</em> . This combinator emits <em>DONE</em> after <em>n</em>
occurrences of <em>TICK</em> . Hint: introduce any necessary instance
variables; use <em>If</em> and appropriate actions.</p>
<pre class="Program">CountDown TICK n DONE =<br>      loop <br>            await TICK ---&gt; i++;<br>                            if (i ==n) emit DONE<br><br><br></pre>
<h3 class="Subproblem">TIMEOUT</h3>
<p class="Subproblem">Write a combinator <em>Timeout</em> parameterized
by an expression <em>e</em> , an event <em>TICK</em> , and an integer <em>n</em>
. This combinator starts as <em>e</em> but kills it after <em>n</em>
occurrences of <em>TICK</em> . Hint: use <em>CountDown</em> .<em> </em></p>
<pre class="Program">Timeout e TICK n =<br>       local DONE in<br>              do e watching DONE<br>              ||<br>              Countdown(TICK,n,DONE)<br></pre>
</body>
</html>
